// There's no need to edit anything here.

local LastAdv = 0
local NextAdvert = 0
function AdvertBegin()
	if AMax.Adverts.Enabled == 1 then
		local Adverts = AMax.Files.ADVERTS
		Adverts = string.Explode("\n", Adverts)
		LastAdv = LastAdv + 1
		if LastAdv + 1 > #Adverts then
				LastAdv = 1
		end
		NextAdvert = Adverts[LastAdv + 1]
			FormatAdvert( NextAdvert )
	end
end

function FormatAdvert( Advert )
	local cvarRep = ""
	if string.Left(Advert, 2) == "//"  then
		AdvertBegin()
	else
		Advert = string.gsub(Advert, "#AMAXVER#", AMax.Ver.Maj ..".".. AMax.Ver.Min )
		Advert = string.gsub(Advert, "#MAP#", game.GetMap )
		Advert = string.gsub(Advert, "#NEXTMAP#", game.GetMapNext )
		Advert = string.gsub(Advert, "#GAMEMODE#", gmod.GetGamemode().Name )
		Advert = string.gsub(Advert, "#HOSTNAME#", GetConVarString("hostname") )
		//ConVar Stuff.
		local a = string.Explode(" ", Advert)
		for i = 0,#a do
			if string.find(a[i], "{") then
				string.gsub(a[i], "{", "")
				string.gsub(a[i], "}", "")
				cvardef = a[i]:GetDefault();
				if type(cvardef) == "float" then
					cvarRep = a[i]:GetFloat()
				elseif type(cvardef) == "bool" then
					cvarRep = a[i]:GetBool()
				elseif type(cvardef) == "string" then
					cvarRep = a[i]:GetString()
				elseif type(cvardef) == "int" then
					cvarRep = a[i]:GetInt()
				end
			end
		end
		SendAdvert( Advert )
	end
end

function SendAdvert(Advert)
	for _,ply in pairs(player.GetAll()) do
		ply:ChatPrint( Advert )
	end
end